package com.nhncorp.yammer.bo;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import com.nhncorp.yammer.bean.Comment;
import com.nhncorp.yammer.bean.CommentColumn;
import com.nhncorp.yammer.bean.Post;
import com.nhncorp.yammer.bean.PostColumn;
import com.nhncorp.yammer.bean.PostType;
import com.nhncorp.yammer.dao.CommentDao;
import com.nhncorp.yammer.dao.CommonDao;
import com.nhncorp.yammer.dao.PostDao;
import com.nhncorp.yammer.util.Datetime;

public class YammerBO {
	@Autowired
	CommonDao commonDao;

	@Autowired
	PostDao postDao;

	@Autowired
	CommentDao commentDao;

	public void insertPostKey(int postKey) {
		String key = String.valueOf(postKey);
		commonDao.insert("post", PostColumn.POSTKEY.getName(), key);
	}

	public void insertCommentKey(int commentKey) {
		String key = String.valueOf(commentKey);
		commonDao.insert("comment", CommentColumn.COMMENTKEY.getName(), key);
	}

	public int nextPostKey() {
		String currentKey = commonDao.get("post", PostColumn.POSTKEY.getName());
		return Integer.parseInt(currentKey) + 1;
	}

	public int nextCommentKey() {
		String currentKey = commonDao.get("comment", CommentColumn.COMMENTKEY.getName());
		return Integer.parseInt(currentKey) + 1;
	}

	public void insertPost(Post post) {
		post.setPostKey(nextPostKey());

		String key = String.valueOf(post.getPostKey());
		postDao.insert(key, PostColumn.POSTKEY.getName(), key);
		postDao.insert(key, PostColumn.CONTENT.getName(), post.getContent());
		postDao.insert(key, PostColumn.USERID.getName(), post.getUserId());
		postDao.insert(key, PostColumn.CREATEDATE.getName(), post.getCreateDate().toString());
		postDao.insert(key, PostColumn.POSTTYPE.getName(), post.getPostType().toString());

		insertPostKey(post.getPostKey());
	}

	public void insertComment(Comment comment) {
		comment.setCommentKey(nextCommentKey());

		String key = String.valueOf(comment.getCommentKey());
		commentDao.insert(key, CommentColumn.COMMENTKEY.getName(), key);
		commentDao.insert(key, CommentColumn.CONTENT.getName(), comment.getContent());
		commentDao.insert(key, CommentColumn.USERID.getName(), comment.getUserId());
		commentDao.insert(key, CommentColumn.POSTKEY.getName(), String.valueOf(comment.getPostKey()));
		commentDao.insert(key, CommentColumn.CREATEDATE.getName(), comment.getCreateDate().toString());

		Post post = selectPost(String.valueOf(comment.getPostKey()));
		post.addCommentKey(comment.getCommentKey());
		postDao.insert(String.valueOf(comment.getPostKey()), PostColumn.COMMENTKEYLIST.getName(), post.getCommentKeyList().toString());

		insertCommentKey(comment.getCommentKey());
	}

	public Post selectPost(String key) {
		String postKey = postDao.get(key, PostColumn.POSTKEY.getName());
		String content = postDao.get(key, PostColumn.CONTENT.getName());
		String userId = postDao.get(key, PostColumn.USERID.getName());
		String createDate = postDao.get(key, PostColumn.CREATEDATE.getName());
		String postType = postDao.get(key, PostColumn.POSTTYPE.getName());
		String commentKeys = postDao.get(key, PostColumn.COMMENTKEYLIST.getName());

		List<Integer> commentKeyList = new ArrayList<Integer>();
		if (StringUtils.isNotBlank(commentKeys)) {
			commentKeys = commentKeys.replace("[", "");
			commentKeys = commentKeys.replace("]", "");
			if (commentKeys.contains(", ")) {
				String[] token = commentKeys.split(", ");
				for (int i = 0; i < token.length; i++) {
					commentKeyList.add(Integer.parseInt(token[i]));
				}
			} else {
				commentKeyList.add(Integer.parseInt(commentKeys));
			}
		}

		return new Post(Integer.parseInt(postKey), content, userId, new Datetime(createDate), PostType.fromValue(Integer.parseInt(postType)), commentKeyList);
	}

	public Comment selectComment(String key) {
		String commentKey = commentDao.get(key, CommentColumn.COMMENTKEY.getName());
		String content = commentDao.get(key, CommentColumn.CONTENT.getName());
		String userId = commentDao.get(key, CommentColumn.USERID.getName());
		String postKey = commentDao.get(key, CommentColumn.POSTKEY.getName());
		String createDate = commentDao.get(key, CommentColumn.CREATEDATE.getName());

		return new Comment(Integer.parseInt(commentKey), content, userId, Integer.parseInt(postKey), new Datetime(createDate));
	}
}